// @algorithm @lc id=442 lang=cpp 
// @title find-all-duplicates-in-an-array


// @test([4,3,2,7,8,2,3,1])=[2,3]
// @test([1,1,2])=[1]
// @test([1])=[]
// @test([5,4,6,7,9,3,10,9,5,6])=[9,5,6]
class Solution {
public:
    vector<int> findDuplicates(vector<int>& nums) {
        vector<int> ans;
        for(auto s : nums){
            if(s<0) s = -s;
            if(nums[s-1]<0)
                ans.push_back(s);
            nums[s-1] = -nums[s-1];
        }
        return ans;
    }
};